<?php

namespace app\lnk\controller\api;

use think\Request;
use app\appcenter\lib\AppIHookService;
use app\lnk\model\DynamicTopicModel;
use app\lnk\model\TopicModel;
use app\lnk\model\DynamicModel;
use app\lnk\model\UserModel;
use app\lnk\service\DynamicService;

use app\lnk\service\OperService;

class Topic
{


    public function dynamic_topic(Request $request)
    {
        $uid = $request->uid;
        $k = $request->post('k','');
        $where[] = ['status', '=', 1];
        $count = 1;
        if(!empty($k)){
            $where[] = ['name', 'like', "$k%"];
            $count = TopicModel::where('name',$k)->count();
        }
        $data = TopicModel::where($where)
            ->order('weigh', 'desc')
            ->field('id,name,dynamic_count,view_count')
            ->limit(20)
            ->select();
        return success('成功', ['data'=>$data ?? [],'count'=>$count]);
    }

    public function get_topic_details(Request $request)
    {

        $id = $request->get('id');
        $uid = $request->uid;
        $data = TopicModel::where('status', 1)
            ->where('id', $id)
            ->find();
        if ($data) {
            return success('成功', $data);
        } else {
            return error('未找到话题或话题异常！', 401);
        }


    }

    public function get_topic_dynamic(Request $request)
    {
        $uid = $request->uid;
        $id = $request->get('id');
        $type = $request->get('type', 0);
        $order = ['d.top' => 'desc', 'd.create_time' => 'desc', 'd.weigh' => 'desc', 'd.view' => 'desc'];
        if ($type == 1) $order = ['d.create_time' => 'desc', 'd.weigh' => 'desc', 'd.view' => 'desc'];
        if ($type == 2) $order = ['d.weigh' => 'desc', 'd.create_time' => 'desc', 'd.view' => 'desc'];
        $data = DynamicModel::alias('d')
            ->where('d.status', 1)
            ->join('sns_dynamic_topic t', 'd.id = t.dynamic_id')
            ->where('d.show', 1)
            ->where('t.topic_id', $id)
            ->order($order)
            ->field('d.*')
            ->paginate(6)->toArray();
        $data = DynamicService::dynamic_map($data, true, $uid);

        return success('成功', $data);
    }



}
